package cn.codeforfun.jfinalplugin.activemq.core;

import com.jfinal.kit.StrKit;
import com.jfinal.log.Log;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * Created by dairymix on 17/4/17.
 */
public abstract class JFinalActiveMq implements Runnable {

    private static Log log = Log.getLog(JFinalActiveMq.class);

    private String user = ActiveMQConnection.DEFAULT_USER;
    private String password = ActiveMQConnection.DEFAULT_PASSWORD;
    private String brokerUrl = ActiveMQConnection.DEFAULT_BROKER_URL;
    private String mqName = null;
    private ConnectionFactory connectionFactory;
    private Connection connection = null;
    private Session session;
    private Destination destination;
    private MessageProducer producer;
    private MessageConsumer consumer;
    private boolean isStarted = false;

//    public JFinalActiveMq(String mqName) {
//        init(mqName);
//    }

    public JFinalActiveMq setUser(String user) {
        this.user = user;
        return this;
    }

    public JFinalActiveMq setPassword(String password) {
        this.password = password;
        return this;
    }

    public JFinalActiveMq setBrokerUrl(String brokerUrl) {
        this.brokerUrl = brokerUrl;
        return this;
    }

    public JFinalActiveMq setMqName(String mqName) {
        this.mqName = mqName;
        return this;
    }

    public String getMqName() {
        return mqName;
    }

    public Session getSession() { return session; }

    public Destination getDestination() { return destination; }

    public MessageConsumer getConsumer() { return consumer().consumer; }

    public MessageProducer getProducer() { return producer; }

    public boolean isStarted() { return isStarted; }

    public static boolean check_message(String message){
        if (StrKit.isBlank(message)) {
            log.debug("Message is blank.");
            return false;
        }
        return true;
    }

    public JFinalActiveMq connection(){
        connectionFactory = new ActiveMQConnectionFactory(
                ActiveMQConnection.DEFAULT_USER,
                ActiveMQConnection.DEFAULT_PASSWORD,
                ActiveMQConnection.DEFAULT_BROKER_URL);
        try { // 构造从工厂得到连接对象
            connection = connectionFactory.createConnection();
            connection.start(); // 启动
        } catch (JMSException e) {
            e.printStackTrace();
        }
        return this;
    }

    public JFinalActiveMq destination(){
        try { // 获取操作连接
            session = connection.createSession(Boolean.FALSE,
                    Session.AUTO_ACKNOWLEDGE);
            destination = session.createTopic(mqName);
        } catch (JMSException e) {
            e.printStackTrace();
        }
        return this;
    }

    public JFinalActiveMq init(String mqName){
        return setMqName(mqName).init();
    }

    public JFinalActiveMq init(){
        if (mqName == null) {
            log.error("mq name can't be null.");
            throw new NullPointerException();
        } else {
            connection();
            destination();
        }
        return this;
    }

    public JFinalActiveMq consumer(Destination destination){
        try {
            if(consumer == null){
                consumer = session.createConsumer(destination);
            }
        } catch (JMSException e) {
            e.printStackTrace();
        }
        return this;
    }

    public JFinalActiveMq consumer(){
        return consumer(destination);
    }

    public boolean startMq() {
        if (isStarted) return true;
        Thread thread = new Thread(this);
        thread.start();
        isStarted = true;
        return true;
    }

    public boolean stopMq() {
        if (!isStarted) return true;
        try {
            this.connection.close();
        } catch (JMSException e) {
            e.printStackTrace();
        }
        isStarted = false;
        return true;
    }

    public abstract void run();
}
